<?php
defined('_VISIT_URL_') or exit('No direct script access allowed');

class test {

	//保存图片
	public function save() {
		$rst = new sqlcon();
		$uuid = '';

		if (getPost('flag') == 'add') {
			$uuid = util::getUUID();

			//保存详情图
	        $bigPicList = json_decode($_POST['bigpic'], true)[0];
	        $coverPath = '';
	        $index = 0;
	        foreach ($bigPicList as $key => $value) {
	        	if (strpos($value, ';base64,')) {
	        		//确定图片路径，并保存图片
	        		$picPath = 'upload/test/samplepic/' . util::getFileName($value);
		            util::saveFile($value, _IMGS_PATH_ . $picPath);

		            //若是封面图，压缩并保存
		            $iscover = 0;
		            if ($key == getPost('coverid')) {
		            	$coverPath = 'upload/test/samplepic/' . util::getFileName($value);
		            	$fileSize = util::getFileSize($value);
		            	$iscover = 1;

		            	require('php/library/compress.php');
		            	if ($fileSize > 500) {
			            	(new compress($value, 1))->compressImg(_IMGS_PATH_ . $coverPath);
			            }
			            else {
		                    util::saveFile($value, _IMGS_PATH_ . $coverPath);
		                }
		            }

		            //保存图片记录
		            $rst->addCollect('testid', $uuid);
		            $rst->addCollect('picid', $key);
		            $rst->addCollect('path', $picPath);
		        	$rst->addCollect('sx', ++$index);
		        	$rst->addCollect('iscover', $iscover);
		        	$rst->insertInfo('testpic');
	        	}
	        }

	        //若有自定义封面图，则单独保存
	        if (empty(getPost('coverid')) && !empty(getPost('coverpic'))) {
	        	if (strpos(getPost('coverpic'), ';base64,')) {
		            $coverPath = 'upload/test/samplepic/' . util::getFileName(getPost('coverpic'));
		            util::saveFile(getPost('coverpic'), _IMGS_PATH_ . $coverPath);
		        }
	        }

	        //保存信息
			$rst->addCollect('testid', $uuid);
			$rst->addCollect('coverpic', $coverPath);
			$rst->insertInfo('test');
		}
		else {
			if (empty(getPost('testid'))) {
				return util::getValidError('ID格式错误');
			}

			$uuid = getPost('testid');
			$bigPicList = json_decode($_POST['bigpic'], true)[0];

			//取出原产品中所有的详情大图路径，若本次保存时已不存在，则删除
			$list = $rst->getList('id, path', 'testpic', "testid = '" . $uuid . "'");
	        for ($i = 0; $i < count($list); $i++) {
				$isDel = 'yes';
				foreach ($bigPicList as $value) {
					if (_HTTP_PATH_ . $list[$i]['path'] == $value) {
						$isDel = 'no';
					}
		        }

		        if ($isDel == 'yes') {
		        	$file_path = _IMGS_PATH_ . $list[$i]['path'];
		        	util::deleteFile($file_path);

					//删除该图片对应的数据库记录
					$rst->addCollect('id', $list[$i]['id']);
					$rst->deleteInfo('testpic');
		        }
			}

			//清除原先的封面图标识
        	$rst->addCollect('iscover', '0');
			$rst->addCollect('testid', $uuid);
			$rst->updateInfo('testpic');

			//保存详情图
			$coverPath = '';
	        $index = 0;
	        foreach ($bigPicList as $key => $value) {
	        	if (strpos($value, ';base64,')) {
	        		//确定图片路径，并保存图片
	        		$picPath = 'upload/test/samplepic/' . util::getFileName($value);
		            util::saveFile($value, _IMGS_PATH_ . $picPath);

		            //若是封面图，压缩并保存
		            $iscover = 0;
		            if ($key == getPost('coverid')) {
		            	$coverPath = 'upload/test/samplepic/' . util::getFileName($value);
		            	$fileSize = util::getFileSize($value);
		            	$iscover = 1;

		            	require('php/library/compress.php');
		            	if ($fileSize > 500) {
			            	(new compress($value, 1))->compressImg(_IMGS_PATH_ . $coverPath);
			            }
			            else {
		                    util::saveFile($value, _IMGS_PATH_ . $coverPath);
		                }
		            }

		            //保存图片记录
		            $rst->addCollect('testid', $uuid);
		            $rst->addCollect('picid', $key);
		            $rst->addCollect('path', $picPath);
		        	$rst->addCollect('sx', ++$index);
		        	$rst->addCollect('iscover', $iscover);
		        	$rst->insertInfo('testpic');
	        	}
	        	else {
	        		//若是封面图，压缩保存，并生成标识
		            if ($key == getPost('coverid')) {
		            	//获取原图base64码并生成img对象，再压缩并保存
		            	$picPath = str_replace(_HTTP_PATH_, _IMGS_PATH_, $value);
		            	if (file_exists($picPath)) {
		            		$value = util::getFileBase64($picPath);
		            		$coverPath = 'upload/test/samplepic/' . util::getUUID() . '.' . explode('.', $picPath)[1];
			            	$fileSize = util::getFileSize($value);

			            	require('php/library/compress.php');
			            	if ($fileSize > 500) {
				            	(new compress($value, 1))->compressImg(_IMGS_PATH_ . $coverPath);
				            }
				            else {
			                    util::saveFile($value, _IMGS_PATH_ . $coverPath);
			                }
		            	}

		            	//根据picid设置该图的封面图标识，以及更新图片的顺序
				        $rst->setSql("update testpic set iscover = 1, sx = " . (++$index) . " where testid = '" . $uuid . "' and picid = '" . $key . "'");
				        $rst->execute();
		            }
		            else {
		            	//若不是封面图，只更新图片的顺序
			            $rst->setSql("update testpic set sx = " . (++$index) . " where testid = '" . $uuid . "' and picid = '" . $key . "'");
				        $rst->execute();
		            }
	        	}
	        }

	        //若有自定义封面图，则单独保存
	        if (empty(getPost('coverid')) && !empty(getPost('coverpic'))) {
	        	if (strpos(getPost('coverpic'), ';base64,')) {
		            $coverPath = 'upload/test/samplepic/' . util::getFileName(getPost('coverpic'));
		            util::saveFile(getPost('coverpic'), _IMGS_PATH_ . $coverPath);

		            //删除原先的封面图
		            $info = $rst->getInfo('coverpic', 'test', "testid = '" . $uuid . "'");
		            if (!empty($info['coverpic'])) {
		            	$file_path = _IMGS_PATH_ . $info['coverpic'];
		            	util::deleteFile($file_path);
		            }
		        }
		        else {
		            $coverPath = str_replace(_HTTP_PATH_, '', getPost('coverpic'));
		        }
	        }
	        else {
	        	//删除原先的封面图
	            $info = $rst->getInfo('coverpic', 'test', "testid = '" . $uuid . "'");
	            if (!empty($info['coverpic'])) {
	            	$file_path = _IMGS_PATH_ . $info['coverpic'];
	            	util::deleteFile($file_path);
	            }
	        }

	        //保存信息
			$rst->addCollect('coverpic', $coverPath);
			$rst->addCollect('testid', $uuid);
			$rst->updateInfo('test');
		}

		$result = util::getSuccess('success');
		return $result;
	}

	//获取信息
	public function getInfo() {
		if (empty(getPost('testid'))) {
			return util::getValidError('ID格式错误');
		}

		$rst = new sqlcon();

		$info = $rst->getInfo('*', 'test', "testid = '" . getPost('testid') . "'");
		if (empty($info)) {
			return util::getDataError('信息不存在');
		}

		//格式化封面图路径
		if (!empty($info['coverpic'])) {
			$info['coverpic'] = _HTTP_PATH_ . $info['coverpic'];
		}

		//获取商家图片
		$picList = $rst->getList('picid, path, iscover', 'testpic', "testid = '" . getPost('testid') . "'", 'sx asc');
		for ($i = 0; $i < count($picList); $i++) {
			$picList[$i]['path'] = _HTTP_PATH_ . $picList[$i]['path'];
		}
		$info['picList'] = $picList;

		$result = util::getSuccess('success');
		$result['testInfo'] = $info;
		return $result;
	}

}